<?php
/**
 * 咨询管理
 * Date: 2017/6/28
 * Time: 12:32
 */

namespace app\home\controller;

use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Memberda extends BaseSeller
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'home/lang/' . config('default_lang') . '/sellerconsult.lang.php');
    }


    /**
     * 受邀用户列表
     */
    public function index()
    {
        $member_model = model('member');
        $list_consult = array();
        $where = array();
        $search_field_value = input('search_field_value');
        $search_field_name = input('search_field_name');

        $condition = array();
        $memberda_type_name_search = input('memberda_type_name');
        if ($memberda_type_name_search) {
            $condition['memberda_type_name'] = $memberda_type_name_search;
        }

        $enterprise_name_search = input('enterprise_name');
        if ($enterprise_name_search) {
            $condition['enterprise_name'] = $enterprise_name_search;
        }

        $enterprise_type_name_search = input('enterprise_type_name');
        if ($enterprise_type_name_search) {
            $condition['enterprise_type_name'] = $enterprise_type_name_search;
        }

        $memberda_type_name_search = input('memberda_type_name');
        if ($memberda_type_name_search) {
            $condition['memberda_type_name'] = $memberda_type_name_search;
        }
        if ($search_field_value != '') {

            switch ($search_field_name) {
                case 'member_name':
                    $condition['member_name'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
                case 'member_email':
                    $condition['member_email'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
                case 'member_mobile':
                    $condition['member_mobile'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
                case 'member_truename':
                    $condition['member_truename'] = array('like', '%' . trim($search_field_value) . '%');
                    break;
            }
        }
        $search_state = input('search_state');
        switch ($search_state) {
            case 'no_informallow':
                $condition['inform_allow'] = '2';
                break;
            case 'no_isbuy':
                $condition['is_buylimit'] = '0';
                break;
            case 'no_isallowtalk':
                $condition['is_allowtalk'] = '0';
                break;
            case 'no_memberstate':
                $condition['member_state'] = '0';
                break;
        }
        //会员级别
        $member_grade = $member_model->getMemberGradeArr();
        //会员等级
        $search_grade = intval(input('get.search_grade'));
        if ($search_grade > 0 && $member_grade) {
            if (isset($member_grade[$search_grade + 1]['exppoints'])) {
                $condition['member_exppoints'] = array('between', array($member_grade[$search_grade]['exppoints'], $member_grade[$search_grade + 1]['exppoints']));
            } else {
                $condition['member_exppoints'] = array('egt', $member_grade[$search_grade]['exppoints']);
            }
        }

        //排序
        $order = trim(input('get.search_sort'));
        if (!in_array($order, array('member_logintime desc', 'member_loginnum desc'))) {
            $order = 'member_id desc';
        }


        $member = db('member')->where(['member_id' => $_SESSION['home']['member_id']])->find();
        $_SESSION['home']['invite_code'] = $member['invite_code'];
        $condition['invite_parent_code'] = $member['invite_code'];


//        $where['invite_type'] = 8;
        //p($where);die;
        $member_list = $member_model->getMemberList($condition, '*', 10, $order);

        //整理会员信息
        if (is_array($member_list) && !empty($member_list)) {
            foreach ($member_list as $k => $v) {
                $member_list[$k]['member_addtime'] = $v['member_addtime'] ? date('Y-m-d H:i:s', $v['member_addtime']) : '';
                $member_list[$k]['member_logintime'] = $v['member_logintime'] ? date('Y-m-d H:i:s', $v['member_logintime']) : '';
                $member_list[$k]['member_grade'] = ($t = $member_model->getOneMemberGrade($v['member_exppoints'], false, $member_grade)) ? $t['level_name'] : '';
            }
        }

        $this->assign('member_grade', $member_grade);

        $this->assign('search_sort', $order);
        $this->assign('show_page', $member_model->page_info->render());
        $this->assign('member_list', $member_list);
        $this->assign('search_field_name', trim($search_field_name));
        $this->assign('search_field_value', trim($search_field_value));

        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('index');
        /* 设置卖家当前栏目 */
        $this->setSellerCurItem('index');
        return $this->fetch($this->template_dir . 'index');
    }

    /**
     * 设置免费服务时间
     */
    public function set_server_time()
    {
        $member_id = intval(input('member_id'));
        if ($member_id <= 0) {
            $this->error('操作失败');
        }
        if (!request()->isPost()) {
            $member_info = db('member')->where('member_id', $member_id)->where('invite_parent_code', $_SESSION['home']['invite_code'])->find();
            if (empty($member_id) || empty($member_info)) {
                $this->error('操作失败');
            }
            $this->assign('member_info', $member_info);
            return $this->fetch($this->template_dir . 'set_server_time');

        } else {
            $data = input('post.');
            $data['server_da_create_time'] = strtotime($data['server_da_create_time']);
            $data['server_da_end_time'] = strtotime($data['server_da_end_time']);
            $member_id = $data['member_id'];
            $data['is_da_server'] = 1;
            unset($data['member_id']);
            $res = db('member')->where('member_id', $member_id)->data($data)->update();
            if ($res) {
                dsLayerOpenSuccess('操作成功');
            } else {
                dsLayerOpenSuccess('操作失败');
            }


        }

    }

    /**
     * Notes: 添加下级页面
     * User: shen
     * Date: 2020/5/15
     * Time: 9:15
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add_son_member()
    {
        if (!request()->isPost()) {
            return $this->fetch($this->template_dir . 'add_son_member');
        } else {
            $mobile = input('mobile', '');

            if (!$mobile) {
                $this->assign('mobile', $mobile);
                return $this->fetch($this->template_dir . 'add_son_member');
            }
            $member_info = db('member')->where('member_type', 0)->where('member_name', $mobile)->find();
            if ($member_info) {
                $member_info['member_addtime'] = date('Y-m-d H:i:s', $member_info['member_addtime']);
                $member_info['member_logintime'] = date('Y-m-d H:i:s', $member_info['member_logintime']);
                $this->assign('member_info', $member_info);
            }
            $this->assign('mobile', $mobile);
            return $this->fetch($this->template_dir . 'add_son_member');
        }

    }


    /**
     * Notes: 导入下级操作
     * User: shen
     * Date: 2020/5/15
     * Time: 11:46
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add_son()
    {
        $member_id = intval(input('member_id'));
        if ($member_id <= 0) {
            ds_json_encode(10000, '操作失败');
        }
        $son_member_info = db('member')->where('member_id', $member_id)->find();
        if ($son_member_info) {
            if ($son_member_info['invite_parent_code']) {
                if ($son_member_info['invite_parent_code'] == $_SESSION['home']['invite_code']) {
                    ds_json_encode(10000, '请勿重复导入');
                } else {
                    $parent_member_info = db('member')->where('invite_code', $son_member_info['invite_parent_code'])->find();
                    if ($parent_member_info) {
                        if ($parent_member_info['member_type'] == 8) {
                            ds_json_encode(10000, '该用户已绑定其他大客户');
                        } else {
                            $update_data = [
                                'invite_parent_code' => $_SESSION['home']['invite_code'],
                            ];
                            if (!$son_member_info['invite_store_id']) {
                                $update_data['invite_store_id'] = config('store_id');
                            }
                            $res = db('member')->where('member_id', $member_id)->data($update_data)->update();
                            if ($res) {
                                ds_json_encode(10000, '操作成功', '', url('Memberda/index'));
                            } else {
                                ds_json_encode(10000, '操作失败', '', url('Memberda/index'));
                            }
                        }
                    } else {
                        $update_data = [
                            'invite_parent_code' => $_SESSION['home']['invite_code'],
                        ];
                        if (!$son_member_info['invite_store_id']) {
                            $update_data['invite_store_id'] = config('store_id');
                        }
                        $res = db('member')->where('member_id', $member_id)->data($update_data)->update();
                        if ($res) {
                            ds_json_encode(10000, '操作成功', '', url('Memberda/index'));
                        } else {
                            ds_json_encode(10000, '操作失败', '', url('Memberda/index'));
                        }
                    }
                }
            } else {
                #为空直接绑定关系
                $update_data = [
                    'invite_parent_code' => $_SESSION['home']['invite_code'],
                ];
                if (!$son_member_info['invite_store_id']) {
                    $update_data['invite_store_id'] = config('store_id');
                }
                $res = db('member')->where('member_id', $member_id)->data($update_data)->update();
                if ($res) {
                    ds_json_encode(10000, '操作成功', '', url('Memberda/index'));
                } else {
                    ds_json_encode(10000, '操作失败', '', url('Memberda/index'));
                }

            }

        } else {
            ds_json_encode(10000, '操作失败', '', url('Memberda/index'));
        }
    }

    /**
     * Notes:移除下级
     * User: shen
     * Date: 2020/5/15
     * Time: 14:56
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function remove_son_member()
    {
        $member_id = intval(input('member_id'));
        if ($member_id <= 0) {
            ds_json_encode(10000, '操作失败');
        }
        $son_member_info = db('member')->where('member_id', $member_id)->find();
        if ($son_member_info) {
            $update_data = [
                'invite_parent_code' => '',
                'credit_num' => '0.00',
                'credit_status' => '0',
                'server_da_create_time' => '',
                'server_da_end_time' => '',
                'is_da_server' => 0,
                'credit_create_time' => '',
                'repair_money' => '0.00',
                'maintain_money' => '0.00',
                'install_money' => '0.00',
                'dismantle_money' => '0.00',
                'working_status' => 0,
                'memberda_type_id' => '0',
                'memberda_type_name' => '',
                'credit_day' => '0',
            ];

            $res = db('member')->where('member_id', $member_id)->data($update_data)->update();
            if ($res) {
                ds_json_encode(10000, '操作成功', '', url('Memberda/index'));
            } else {
                ds_json_encode(10000, '操作失败', '', url('Memberda/index'));
            }

        } else {
            ds_json_encode(10000, '操作失败', '', url('Memberda/index'));
        }
    }


    /**
     * 清除服务时间
     */
    public function cancel_time()
    {
        $member_id = intval(input('member_id'));
        if ($member_id <= 0) {
            $this->error('操作失败');
        }

        $member_info = db('member')->where('member_id', $member_id)->where('invite_type', 8)->where('inviter_id', $_SESSION['home']['member_id'])->find();
        if (empty($member_id)) {
            $this->error('操作失败');
        }


        $data['server_da_create_time'] = '';
        $data['server_da_end_time'] = '';
        $data['is_da_server'] = 0;

        $res = db('member')->where('member_id', $member_id)->data($data)->update();
        if ($res) {
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10000, '操作失败');
        }


    }

    public function show()
    {
        $enterprise_model = model('enterprise');
        $member_id = input('param.member_id');
        // $enterprise_detail = $enterprise_model->getEnterpriseDetail(array('member_id' => $member_id));
        $enterprise_detail = db('member')->where(['member_id' => $member_id])->find();
        // var_dump($enterprise_detail);exi
        $this->assign('joinin_detail', $enterprise_detail);

        $enterprise_type = db('enterprisetype')->where(['id' => $enterprise_detail['enterprise_type']])->find();
        $this->assign('name', $enterprise_type['name']);
        $config = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $config['value']);
        $this->assign('is_verify', false);
        $this->assign('detail', ['0' => '禁用', '1' => '启用']);
        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('index');
        /* 设置卖家当前栏目 */
        $this->setSellerCurItem('show');
        return $this->fetch($this->template_dir . 'show');
    }

    public function show1()
    {
        $member_id = input('param.member_id');
        if (empty($member_id)) {
            $this->error(lang('param_error'));
        }
        $member_model = model('member');
        $condition['member_id'] = $member_id;
        $member_array = $member_model->getMemberInfo($condition);
        $this->assign('member_array', $member_array);
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        /* 设置卖家当前菜单 */
        $this->setSellerCurMenu('index');
        /* 设置卖家当前栏目 */
        $this->setSellerCurItem('show');
        return $this->fetch($this->template_dir . 'show');

    }


    /**
     * 用户中心右边，小导航
     *
     * @param string $menu_type 导航类型
     * @param string $menu_key 当前导航的menu_key
     * @param array $array 附加菜单
     * @return
     */
    protected function getSellerItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => '客户列表', 'url' => url('Memberda/index'),
            ),

        );
        return $menu_array;
    }
}
